package com.ruoyi.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.app.domain.AppJumpConfig;
import com.ruoyi.app.domain.bo.AppJumpConfigBo;
import com.ruoyi.app.domain.vo.AppJumpConfigVo;
import com.ruoyi.app.mapper.AppJumpConfigMapper;
import com.ruoyi.app.service.IAppJumpConfigService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 跳转配置Service业务层处理
 *
 * @author ruoyi
 * @date 2025-11-25
 */
@RequiredArgsConstructor
@Service
public class AppJumpConfigServiceImpl implements IAppJumpConfigService {

    private final AppJumpConfigMapper baseMapper;

    /**
     * 查询跳转配置
     */
    @Override
    public AppJumpConfigVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询跳转配置列表
     */
    @Override
    public TableDataInfo<AppJumpConfigVo> queryPageList(AppJumpConfigBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppJumpConfig> lqw = buildQueryWrapper(bo);
        Page<AppJumpConfigVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询跳转配置列表
     */
    @Override
    public List<AppJumpConfigVo> queryList(AppJumpConfigBo bo) {
        LambdaQueryWrapper<AppJumpConfig> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppJumpConfig> buildQueryWrapper(AppJumpConfigBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppJumpConfig> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getJumpName()), AppJumpConfig::getJumpName, bo.getJumpName());
        lqw.eq(StringUtils.isNotBlank(bo.getJumpUrl()), AppJumpConfig::getJumpUrl, bo.getJumpUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getJumpLogo()), AppJumpConfig::getJumpLogo, bo.getJumpLogo());
        lqw.eq(bo.getSort() != null, AppJumpConfig::getSort, bo.getSort());
        lqw.eq(bo.getStatus() != null, AppJumpConfig::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增跳转配置
     */
    @Override
    public Boolean insertByBo(AppJumpConfigBo bo) {
        AppJumpConfig add = BeanUtil.toBean(bo, AppJumpConfig.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改跳转配置
     */
    @Override
    public Boolean updateByBo(AppJumpConfigBo bo) {
        AppJumpConfig update = BeanUtil.toBean(bo, AppJumpConfig.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppJumpConfig entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除跳转配置
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
